package com.bawei.mall.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.common.core.utils.JwtUtils;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.user.domain.CusUser;
import com.bawei.mall.user.domain.MallProductAddress;
import com.bawei.mall.user.service.CompletionAddressService;
import com.bawei.mall.user.utils.Addr;
import com.bawei.system.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.user.controller
 * @Project：mall_cloud
 * @name：CompletionAddressController
 * @Date：2024/6/29 11:48
 */
@RestController
@RequestMapping("/address")
public class CompletionAddressController {
    @Autowired
    private CompletionAddressService completionAddressService;
    @Autowired
    private Addr addrs;




    /**
     * 自动识别地址
     */
    @GetMapping("/analyzeAddress")
    public R analyzeAddress(@RequestParam ("addr") String addr){
        String s = addrs.analyzeAddres(addr);
        Map map = JSON.parseObject(s, Map.class);
        return R.ok(map.get("data"));
    }

    /**
     * 添加地址信息
     * @param mallProductAddress
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody MallProductAddress mallProductAddress){
        completionAddressService.add(mallProductAddress);
        return R.ok("添加成功");
    }


    /**
     * 查询地址信息表
     * @return
     */
    @GetMapping("/fetchAddressList")
    public R<List<MallProductAddress>> fetchAddressList(){
        List<MallProductAddress> list=completionAddressService.fetchAddressList();
        return R.ok(list);
    }


    /**
     * 删除地址
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R del(@RequestParam Integer id){
        completionAddressService.del(id);
        return R.ok("删除成功");
    }


    /**
     * 根据id查询具体地址
     * @param id
     * @return
     */
    @GetMapping("/findAddressById")
    public R findAddressById(@RequestParam Integer id){
        MallProductAddress mallProductAddress=completionAddressService.findAddressById(id);
        return R.ok(mallProductAddress);
    }


    /**
     * 修改地址
     * @param mallProductAddress
     * @return
     */
    @PostMapping("/updateAddress")
    public R updateAddress(@RequestBody MallProductAddress mallProductAddress){
        completionAddressService.updateAddress(mallProductAddress);
        return R.ok("修改成功");
    }



}
